package com.tcs.test.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoadFileDirectory {

	private final Logger logger = LoggerFactory.getLogger(LoadFileDirectory.class);

	/**
	 * 读取某个文件夹下的所有文件
	 */
	public boolean readfile(String filepath) throws FileNotFoundException, IOException {
		try {
			File file = new File(filepath);
			if (!file.isDirectory()) {
				logger.info(" 没有下一级目录!");
			} else if (file.isDirectory()) {
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					File readfile = new File(filepath + "\\" + filelist[i]);
					if (!readfile.isDirectory()) {
						logger.info("path  {} , absolutepath : {} , name : {} " , readfile.getPath(), readfile.getAbsolutePath() , readfile.getName());
					} else if (readfile.isDirectory()) {
						readfile(filepath + "\\" + filelist[i]);
					}
				}
			}
		} catch (FileNotFoundException e) {
			logger.error("" , e);
		}
		return true;
	}
	
	/**
	 * 读取某个文件夹下的所有文件
	 */
	public boolean readfile(String filepath ,String repStr) throws FileNotFoundException, IOException {
		boolean result = false;
		try {
			File file = new File(filepath);
			if (!file.isDirectory()) {
				logger.info(" 没有下一级目录!");
			} else if (file.isDirectory()) {
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					File readfile = new File(filepath + "\\" + filelist[i]);
					if (!readfile.isDirectory()) {
						result = readfile.renameTo(new File(readfile.getPath().replace(repStr, "")));
						logger.info(" 文件 : {} , 重命名成功 : {} " , readfile.getPath() , result);
//						logger.info("path  {} , absolutepath : {} , name : {} " , readfile.getPath(), readfile.getAbsolutePath() , readfile.getName());
					} else if (readfile.isDirectory()) {
						readfile(filepath + "\\" + filelist[i] , repStr);
					}
				}
			}
		} catch (FileNotFoundException e) {
			logger.error("" , e);
		}
		return true;
	}

	@Test
	public void main1() {
		try {
			this.readfile("G:\\test\\demo" , 
					"nihao11");
		} catch (FileNotFoundException e) {
			logger.error("" , e);
		} catch (IOException e) {
			logger.error("" , e);
		}
	}
}
